using UnityEngine;

public class CharacterStats : MonoBehaviour
{
    [Header("Major Stats")]
    public Stat strength;//力量  增加伤害
    public Stat agility;//敏捷  增加闪避
    public Stat intelligence;//智力 增加魔法伤害
    public Stat vitality;//生命力 增加生命值

    [Header("Offensive Stats")]
    public Stat damage;
    public Stat critChance; //暴击几率
    public Stat critPower;//暴击伤害

    [Header("Defensive Stats")]
    public Stat maxHp;
    public Stat armor;//护甲
    public Stat evasion;//闪避
    public Stat magicResistance;//魔法抗性

    [Header("Magic Stats")]
    public Stat fireDamage;
    public Stat iceDamage;
    public Stat lightningDamage;


    public bool isIgnited;
    public bool isChilled;
    public bool isShocked;


    public int currentHp;
    public System.Action onHpChanged;

    protected virtual void Start()
    {
        critPower.SetDefaultValue(150);
        currentHp = GetMaxHpValue();

    }

    public virtual void DoDamage(CharacterStats _targetStats)
    {
        if (TargetCanAvoidAttack(_targetStats)) return;

        int totalDamage = damage.GetValue() + strength.GetValue();


        if (CanCrit())
        {
            totalDamage = CalculateCritDamage(totalDamage);
        }

        totalDamage = CheckTargetArmor(_targetStats, totalDamage);
        _targetStats.TakeDamage(totalDamage);
    }
    //造成魔法伤害
    public virtual void DoMagicDamage(CharacterStats _targetStats)
    {

    }

    //施加负面效果
    public void ApplyAilments(bool _ignite, bool _chill, bool _shock)
    {

        if (isIgnited || isChilled || isShocked) return;

        isIgnited = _ignite;
        isChilled = _chill;
        isShocked = _shock;
    }



    public virtual void TakeDamage(int _damage)
    {
        DecreaseHpBy(_damage);
        if (currentHp <= 0)
        {
            Die();
        }
    }

    protected virtual void DecreaseHpBy(int _damage)
    {
        currentHp -= _damage;
        if (onHpChanged != null)
        {
            onHpChanged();
        }
    }


    //是否可以闪避攻击
    private bool TargetCanAvoidAttack(CharacterStats _targetStats)
    {
        int totalEvasion = _targetStats.evasion.GetValue() + _targetStats.agility.GetValue();
        if (Random.Range(0, 100) < totalEvasion)
        {
            return true;
        }
        return false;
    }

    //检查护甲
    private int CheckTargetArmor(CharacterStats _targetStats, int totalDamage)
    {
        totalDamage -= _targetStats.armor.GetValue();
        totalDamage = Mathf.Clamp(totalDamage, 0, int.MaxValue);
        return totalDamage;
    }

    //是否暴击
    private bool CanCrit()
    {
        int totalCritChance = critChance.GetValue() + agility.GetValue();

        if (Random.Range(0, 100) < totalCritChance)
        {
            return true;
        }
        return false;
    }

    //计算暴击伤害
    private int CalculateCritDamage(int _damage)
    {
        float totalCritPower = (critPower.GetValue() + strength.GetValue()) * .01f;
        float critDamage = _damage * totalCritPower;
        return Mathf.RoundToInt(critDamage);

    }
    protected virtual void Die()
    {

    }

    public int GetMaxHpValue()
    {
        return maxHp.GetValue() + vitality.GetValue() * 5;
    }
}
